package com.glorysoft.sunnypda.ui.reportrepair

import android.os.Bundle
import androidx.databinding.ObservableArrayList
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseViewModel
import com.glorysoft.sunnypda.base.Constant
import com.glorysoft.sunnypda.entity.RepairInfoEntity
import com.glorysoft.sunnypda.entity.ui.CallObserveBean
import com.glorysoft.sunnypda.entity.ui.ToastMessageBean
import com.glorysoft.sunnypda.model.RepairRepository
import com.google.gson.JsonObject
import me.tatarka.bindingcollectionadapter2.BR
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: ReportRepairViewModel
 * Author: XJH
 * Date: 2022/1/7 16:13
 * Description:
 * History:
 */
class ReportRepairViewModel : BaseViewModel() {

    val repairRepository : RepairRepository by lazy { RepairRepository() }
    var repairAllList : ArrayList<RepairInfoEntity> = arrayListOf()

    val onClick : ReportClick = object : ReportClick{
        override fun onReportItemClick(item: RepairInfoEntity) {
            val bundle = Bundle()
            bundle.putString("RFPID",item.RFR_ID)
            defUI.startActivity.value = bundle
        }

    }

    var repairItems = ObservableArrayList<RepairInfoEntity>()
    val unFinishList = ArrayList<RepairInfoEntity>()
    val finishList = ArrayList<RepairInfoEntity>()
    val runningList = ArrayList<RepairInfoEntity>()
    var repairBinding =
        ItemBinding.of<RepairInfoEntity>(BR.repairItem, R.layout.item_repair_list)
            .bindExtra(BR.onReportClick,onClick)


    fun initRepairForUser(status : Int,eqp: String){
        val map = JsonObject()
        map.addProperty("workNo", Constant.CURRENT_USER)
        map.addProperty("name", Constant.CURRENT_USER_NAME)
        launchOnlyResult({
            val repairListFroUser = repairRepository.getRepairListFroUser2(map)
            repairListFroUser.RFR_LIST.forEach{
//                when(it.RFR_TYPE){
//                    "0" -> it.RepairType = "一般故障"
//                    "1" -> it.RepairType = "设备问题"
//                    "2" -> it.RepairType = "程序问题"
//                }
                it.RepairType = if(it.RFR_TYPE == null)"" else it.RFR_TYPE
            }
            repairAllList.clear()
            repairAllList.addAll(repairListFroUser.RFR_LIST)
            updateList(status,eqp)
        })
    }

    fun initRepairForEqp(status : Int,eqp : String){
        updateList(status,eqp)
    }


    fun updateList(status: Int,eqp : String) {
        repairItems.clear()
        unFinishList.clear()
        finishList.clear()
        runningList.clear()
        val repairAllList1 = getMaoPaoPaiXuList(repairAllList)
        if (repairAllList1.isNotEmpty()){
            repairAllList1.forEach {
                if (eqp.isEmpty()){
                    if (it.RFR_CURR_STATE == status){
                        repairItems.add(it)
                    }
                    when (it.RFR_CURR_STATE) {
                        0 -> {
                            unFinishList.add(it)
                        }
                        1 -> {
                            runningList.add(it)
                        }
                        2 ->{
                            finishList.add(it)
                        }
                    }
                }else{
                    if (it.RFR_CURR_STATE == status && it.EQP_ID == eqp){
                        repairItems.add(it)
                    }
                    when (it.RFR_CURR_STATE) {
                        0 -> {
                            if (it.EQP_ID == eqp){
                                unFinishList.add(it)
                            }
                        }
                        1 -> {
                            if (it.EQP_ID == eqp){
                                runningList.add(it)
                            }
                        }
                        2 ->{
                            if (it.EQP_ID == eqp){
                                finishList.add(it)
                            }
                        }
                    }
                }

            }
            defUI.callObserve.value = CallObserveBean(0)
        }else{
            defUI.toastEvent.value = ToastMessageBean("服务器未返回数据",false)
        }
    }

    @JvmName("getRepairAllList1")
    fun getMaoPaoPaiXuList(list:ArrayList<RepairInfoEntity>): ArrayList<RepairInfoEntity> {
        val repairList  = ArrayList<RepairInfoEntity>()
        val id = ArrayList<Int>()
        for (i in 0..list.size-1){
            val idi = list[i].RFR_ID.toInt()
            id.add(idi)
        }
        for (i in 1..id.size){
            for (j in 0..id.size-2){
                if (id[j] > id[j+1]){
                    val sort = id[j]
                    id[j] = id[j+1]
                    id[j+1] = sort
                }
            }
        }
        for (i in id) {
            list.forEach {
                val rfr_id = it.RFR_ID.toInt()
                if (i == rfr_id){
                    repairList.add(it)
                }
            }
        }

        return repairList
    }


    interface ReportClick{
        fun onReportItemClick(item : RepairInfoEntity)
    }

}